package com.cabrida.livestar.mainstar.setlive;

import java.io.File;
import java.io.FileNotFoundException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import org.apache.http.Header;
import org.json.JSONObject;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.provider.MediaStore.Images;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.TimePicker.OnTimeChangedListener;

import com.cabrida.livestar.BaseActivity;
import com.cabrida.livestar.R;
import com.cabrida.livestar.config.Global;
import com.cabrida.livestar.config.HttpHelp;
import com.cabrida.livestar.config.Urls;
import com.cabrida.livestar.mainstar.MainStarActivity;
import com.cabrida.livestar.model.StarAdvanceData;
import com.cabrida.livestar.util.Bimp;
import com.cabrida.livestar.util.FileBitmapUtils;
import com.cabrida.livestar.util.FileUtils;
import com.cabrida.livestar.util.HttpUtils;
import com.cabrida.livestar.util.ImageUtils;
import com.cabrida.livestar.util.JsonUtils;
import com.cabrida.livestar.util.L;
import com.cabrida.livestar.util.MyUtils;
import com.cabrida.livestar.util.NetworkUtils;
import com.cabrida.livestar.util.StringUtils;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;

/**
 * 明星端直播设定
 * 
 * @author why
 */
public class SetLiveActivity extends BaseActivity {
	public static final String TAG = SetLiveActivity.class.getSimpleName();
	private ImageView iv_back;
	private TextView tv_title;
	private TextView tv_right;
	
	private ImageView iv_bg;
	private LinearLayout ll_bg;

	private LinearLayout ll_date;
	private LinearLayout ll_time;
	private LinearLayout ll_price;
	private TextView tv_date;
	private TextView tv_time;
	private TextView tv_price;
	
	private Dialog dialogDate;
	private Dialog dialogTime;
	private Dialog dialogPrice;
	
	private String Year="";
	
	private LinearLayout ll_red_packet;
	private LinearLayout ll_gift;
	private ImageView iv_red_packet_choice;
	private ImageView iv_gift_choice;
	private boolean isFlag_red=false;
	private boolean isFlag_gift=false;
	private StarAdvanceData advData;
	
	private Dialog dialogHead;

	private String protraitPath;
	private Bitmap protraitBitmap;
	private File protraitFile;
	private final static String FILE_SAVEPATH = FileBitmapUtils.SDPATH;
	private Uri origUri;
	private Uri cropUri;
	private final static int CROP = 600;
	private String str_path="";
	private String str_file="";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

	}
	
	/**
	 * 上传新照片
	 */
	private void uploadNewPhoto() {
		// TODO Auto-generated method stub
		RequestParams params = new RequestParams();
		params.put("udid", Global.U_id);
		params.put("token", Global.U_token);
		try {
			params.put("img", protraitFile);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if (!NetworkUtils.isNetAvailable(context)) {
			showToast(R.string.no_network);
			return;
		}
		showProgress();
		HttpUtils.getClient().post(Urls.GET_IMGUPLOAD_URL, params,
				new JsonHttpResponseHandler() {

					@Override
					public void onSuccess(int statusCode, Header[] headers,
							JSONObject response) {
						dismissProgress();
						L.e(TAG, "uploadNewPhoto", response.toString());

						int code = JsonUtils.getInt(response, "code", -1);
						//
						if (code == HttpHelp.Succeed) {
							JSONObject data = JsonUtils.getJSONObject(response, "data", new JSONObject());
							str_path = JsonUtils.getString(data,"path", "");
							str_file = JsonUtils.getString(data,"file", "");
							protraitBitmap = Bimp.getSmallBitmap(protraitPath);
							iv_bg.setImageBitmap(protraitBitmap);
						} else {
							JSONObject data = JsonUtils.getJSONObject(response,
									"data", new JSONObject());
							String message = JsonUtils.getString(data,
									"message", "");
							showToast(message);
						}
					}

					@Override
					public void onFailure(int statusCode, Header[] headers,
							String responseString, Throwable throwable) {
						dismissProgress();
						showToast(getResources().getString(R.string.fwq_error));
					}
				});
	}

	@Override
	protected void findViewById() {
		// TODO Auto-generated method stub
		setContentView(R.layout.activity_main_star_setlive);
		iv_back = (ImageView) findViewById(R.id.iv_back);
		tv_title = (TextView) findViewById(R.id.tv_title);
		tv_right = (TextView) findViewById(R.id.tv_right);
		iv_bg = (ImageView) findViewById(R.id.iv_bg);
		ll_bg = (LinearLayout) findViewById(R.id.ll_bg);
		ll_date = (LinearLayout) findViewById(R.id.ll_date);
		ll_time = (LinearLayout) findViewById(R.id.ll_time);
		ll_price = (LinearLayout) findViewById(R.id.ll_price);
		tv_date = (TextView) findViewById(R.id.tv_date);
		tv_price = (TextView) findViewById(R.id.tv_price);
		tv_time = (TextView) findViewById(R.id.tv_time);
		ll_red_packet = (LinearLayout) findViewById(R.id.ll_red_packet);
		ll_gift = (LinearLayout) findViewById(R.id.ll_gift);
		iv_red_packet_choice = (ImageView) findViewById(R.id.iv_red_packet_choice);
		iv_gift_choice = (ImageView) findViewById(R.id.iv_gift_choice);
	}

	@Override
	protected void initView() {
		// TODO Auto-generated method stub
		iv_back.setOnClickListener(this);
		tv_title.setText("设定");
		tv_right.setText("发布");
		tv_right.setOnClickListener(this);
		ll_date.setOnClickListener(this);
		ll_time.setOnClickListener(this);
		ll_price.setOnClickListener(this);
		ll_red_packet.setOnClickListener(this);
		ll_gift.setOnClickListener(this);
		ll_bg.setOnClickListener(this);
		getAdvanceData();
	}
	
	private void setAdvanceData() {
		MyUtils.loadImg(advData.getS_image().toString(), iv_bg);
		tv_price.setText(advData.getS_price().toString());
		if ("0".equals(advData.getEdit())) {
				ll_price.setEnabled(false);
		}
	}
	
	/** 获取数据 */
	private void getAdvanceData() {
		// TODO Auto-generated method stub
		RequestParams params = new RequestParams();
		params.put("udid", Global.U_id);
		params.put("token", Global.U_token);
		if (!NetworkUtils.isNetAvailable(context)) {
			showToast(R.string.no_network);
			return;
		}
		showProgress();
		HttpUtils.getClient().post(Urls.GET_MAIN_STAR_ENTER_ADVANCE_URL, params,
				new JsonHttpResponseHandler() {

					@Override
					public void onSuccess(int statusCode, Header[] headers,
							JSONObject response) {
						dismissProgress();
						L.e(TAG, "getAdvanceData", response.toString());
						
						int code = JsonUtils.getInt(response, "code", -1);
						//
						if (code == HttpHelp.Succeed) {
							advData = new StarAdvanceData();
							JSONObject data = JsonUtils.getJSONObject(response, "data", new JSONObject());
							advData.setS_udid(JsonUtils.getString(data,"s_udid", ""));
							advData.setS_name(JsonUtils.getString(data,"s_name", ""));
							advData.setS_price(JsonUtils.getString(data,"s_price", ""));
							advData.setS_image(JsonUtils.getString(data,"s_image", ""));
							advData.setS_lasttime(JsonUtils.getString(data,"s_lasttime", ""));
							advData.setEdit(JsonUtils.getString(data,"edit", ""));
							setAdvanceData();
						} else {
							JSONObject data = JsonUtils.getJSONObject(response, "data", new JSONObject());
							String message=JsonUtils.getString(data,"message", "");
							showToast(message);
						}
					}

					@Override
					public void onFailure(int statusCode, Header[] headers,
							String responseString, Throwable throwable) {
						dismissProgress();
						showToast(getResources().getString(R.string.fwq_error));
					}
				});
	}
	
	/** 创建预播数据 */
	private void getPublishData(String date,String star,String price,String redpacket,String gift) {
		// TODO Auto-generated method stub
		RequestParams params = new RequestParams();
		params.put("udid", Global.U_id);
		params.put("token", Global.U_token);
		params.put("date", Year+"-"+date);
		params.put("star", star);
		params.put("end", "0");
		params.put("price", price);
		params.put("package", redpacket);
		params.put("gift", gift);
		params.put("path", str_path);
		params.put("file", str_file);
		if (!NetworkUtils.isNetAvailable(context)) {
			showToast(R.string.no_network);
			return;
		}
		showProgress();
		HttpUtils.getClient().post(Urls.GET_MAIN_STAR_ADVANCE_URL, params,
				new JsonHttpResponseHandler() {

					@Override
					public void onSuccess(int statusCode, Header[] headers,
							JSONObject response) {
						dismissProgress();
						L.e(TAG, "getPublishData", response.toString());
						
						int code = JsonUtils.getInt(response, "code", -1);
						//
						if (code == HttpHelp.Succeed) {
							JSONObject data = JsonUtils.getJSONObject(response, "data", new JSONObject());
							String message=JsonUtils.getString(data,"message", "");
							showToast(message);
							Intent intent = new Intent();
							intent.setAction(MainStarActivity.ACTION_MAINSTAR_REFRESH);
							sendBroadcast(intent);
							finish();
						} else {
							JSONObject data = JsonUtils.getJSONObject(response, "data", new JSONObject());
							String message=JsonUtils.getString(data,"message", "");
							showToast(message);
						}
					}

					@Override
					public void onFailure(int statusCode, Header[] headers,
							String responseString, Throwable throwable) {
						dismissProgress();
						showToast(getResources().getString(R.string.fwq_error));
					}
				});
	}

	private void showDateDialog() {
		initDateDialogView();
		if (null != dialogDate && dialogDate.isShowing()) {
			return;
		}
		if (null != dialogDate) {
			/**
			 * 等数据加载完毕，显示对话框
			 */
			Window window = dialogDate.getWindow();
			// 设置显示动画
			window.setWindowAnimations(R.style.main_menu_animstyle);
			WindowManager.LayoutParams wl = window.getAttributes();
			wl.x = 0;
			wl.y = ((Activity) context).getWindowManager().getDefaultDisplay()
					.getHeight();
			// 以下这两句是为了保证按钮可以水平满屏
			wl.width = ViewGroup.LayoutParams.MATCH_PARENT;
			wl.height = ViewGroup.LayoutParams.MATCH_PARENT;
			// 设置显示位置
			dialogDate.onWindowAttributesChanged(wl);
			// 设置点击外围解散
			dialogDate.setCanceledOnTouchOutside(true);
			dialogDate.show();
		}
	}

	private void initDateDialogView() {
		LayoutInflater mInflater = LayoutInflater.from(context);
		View view = mInflater
				.inflate(R.layout.dialog_setlive_date_choice, null);

		final DatePicker datePicker = (DatePicker) view
				.findViewById(R.id.datePicker);

		Button btnCancle = (Button) view.findViewById(R.id.btn_cancel);
		btnCancle.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (null != dialogDate) {
					dialogDate.dismiss();
				}
			}
		});
		Button btnSure = (Button) view.findViewById(R.id.btn_sure);
		btnSure.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Year = String.valueOf(datePicker.getYear());
				tv_date.setText( (datePicker.getMonth() + 1) + "-" 	+ datePicker.getDayOfMonth());
				if (null != dialogDate) {
					dialogDate.dismiss();
				}
			}
		});

		dialogDate = new Dialog(context, R.style.dialogWindowStyle);
		dialogDate.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dialogDate.setContentView(view, new LayoutParams(
				LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
	}

	private void showTimeDialog() {
		initTimeDialogView();
		if (null != dialogTime && dialogTime.isShowing()) {
			return;
		}
		if (null != dialogTime) {
			/**
			 * 等数据加载完毕，显示对话框
			 */
			Window window = dialogTime.getWindow();
			// 设置显示动画
			window.setWindowAnimations(R.style.main_menu_animstyle);
			WindowManager.LayoutParams wl = window.getAttributes();
			wl.x = 0;
			wl.y = ((Activity) context).getWindowManager().getDefaultDisplay()
					.getHeight();
			// 以下这两句是为了保证按钮可以水平满屏
			wl.width = ViewGroup.LayoutParams.MATCH_PARENT;
			wl.height = ViewGroup.LayoutParams.MATCH_PARENT;
			// 设置显示位置
			dialogTime.onWindowAttributesChanged(wl);
			// 设置点击外围解散
			dialogTime.setCanceledOnTouchOutside(true);
			dialogTime.show();
		}
	}

	int hour;
	int minute;

	private void initTimeDialogView() {
		LayoutInflater mInflater = LayoutInflater.from(context);
		View view = mInflater
				.inflate(R.layout.dialog_setlive_time_choice, null);

		TimePicker timePicker = (TimePicker) view.findViewById(R.id.timePicker);

		Calendar ca = Calendar.getInstance();
		hour = ca.get(Calendar.HOUR);
		minute = ca.get(Calendar.MINUTE);

		timePicker.setOnTimeChangedListener(new OnTimeChangedListener() {

			@Override
			public void onTimeChanged(TimePicker arg0, int arg1, int arg2) {
				// TODO Auto-generated method stub
				hour = arg1;
				minute = arg2;
			}
		});

		Button btnCancle = (Button) view.findViewById(R.id.btn_cancel);
		btnCancle.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (null != dialogTime) {
					dialogTime.dismiss();
				}
			}
		});
		Button btnSure = (Button) view.findViewById(R.id.btn_sure);
		btnSure.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				tv_time.setText(hour + ":" + minute);
				if (null != dialogTime) {
					dialogTime.dismiss();
				}
			}
		});

		dialogTime = new Dialog(context, R.style.dialogWindowStyle);
		dialogTime.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dialogTime.setContentView(view, new LayoutParams(
				LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
	}

	private void showPriceDialog() {
		initPriceDialogView();
		if (null != dialogPrice && dialogPrice.isShowing()) {
			return;
		}
		if (null != dialogPrice) {
			/**
			 * 等数据加载完毕，显示对话框
			 */
			Window window = dialogPrice.getWindow();
			// 设置显示动画
			window.setWindowAnimations(R.style.main_menu_animstyle);
			WindowManager.LayoutParams wl = window.getAttributes();
			wl.x = 0;
			wl.y = ((Activity) context).getWindowManager().getDefaultDisplay()
					.getHeight();
			// 以下这两句是为了保证按钮可以水平满屏
			wl.width = ViewGroup.LayoutParams.MATCH_PARENT;
			wl.height = ViewGroup.LayoutParams.MATCH_PARENT;
			// 设置显示位置
			dialogPrice.onWindowAttributesChanged(wl);
			// 设置点击外围解散
			dialogPrice.setCanceledOnTouchOutside(true);
			dialogPrice.show();
		}
	}

	private void initPriceDialogView() {
		LayoutInflater mInflater = LayoutInflater.from(context);
		View view = mInflater.inflate(R.layout.dialog_setlive_price_choice,
				null);

		ListView listView = (ListView) view.findViewById(R.id.listView);
		PriceLvAdapter adapter = new PriceLvAdapter(context);
		listView.setAdapter(adapter);
		ArrayList<String> list = new ArrayList<String>();
		for (int i = 1; i <= 10; i++) {
			list.add(i + "元");
		}
		adapter.setData(list);
		adapter.notifyDataSetChanged();

		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				tv_price.setText(arg0.getAdapter().getItem(arg2).toString());
				if (null != dialogPrice) {
					dialogPrice.dismiss();
				}
			}
		});

		dialogPrice = new Dialog(context, R.style.dialogWindowStyle);
		dialogPrice.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dialogPrice.setContentView(view, new LayoutParams(
				LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
	}

	class PriceLvAdapter extends BaseAdapter {

		private LayoutInflater mInflater;
		private ArrayList<String> data_list = new ArrayList<String>();

		public PriceLvAdapter(Context context) {
			this.mInflater = LayoutInflater.from(context);
		}

		public void setData(ArrayList<String> data) {
			data_list = data;
		}

		@Override
		public int getCount() {
			return data_list.size();
		}

		@Override
		public String getItem(int arg0) {
			return data_list.get(arg0);
		}

		@Override
		public long getItemId(int arg0) {
			return arg0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {

			ViewHolder holder = null;
			if (convertView == null) {

				holder = new ViewHolder();

				convertView = mInflater.inflate(
						R.layout.adapter_setlive_price_lv_item, null);
				holder.tv_price_item = (TextView) convertView
						.findViewById(R.id.tv_price_item);
				convertView.setTag(holder);

			} else {
				holder = (ViewHolder) convertView.getTag();

			}
			holder.tv_price_item.setText(data_list.get(position).toString());
			return convertView;
		}

		class ViewHolder {
			private TextView tv_price_item;
		}

	}
	
	/**
	 * 头像点击事件
	 */
	private void img_head_perfection_click() {
		showHeadDialog((Activity) context);
	}

	private void showHeadDialog(Activity activity) {
		initHeadDialogView();
		if (null != dialogHead && dialogHead.isShowing()) {
			return;
		}
		if (null != dialogHead) {
			/**
			 * 等数据加载完毕，显示对话框
			 */
			Window window = dialogHead.getWindow();
			// 设置显示动画
			window.setWindowAnimations(R.style.main_menu_animstyle);
			WindowManager.LayoutParams wl = window.getAttributes();
			wl.x = 0;
			wl.y = ((Activity) context).getWindowManager().getDefaultDisplay()
					.getHeight();
			// 以下这两句是为了保证按钮可以水平满屏
			wl.width = ViewGroup.LayoutParams.MATCH_PARENT;
			wl.height = ViewGroup.LayoutParams.MATCH_PARENT;
			// 设置显示位置
			dialogHead.onWindowAttributesChanged(wl);
			// 设置点击外围解散
			dialogHead.setCanceledOnTouchOutside(true);
			dialogHead.show();
		}
	}

	private void initHeadDialogView() {
		LayoutInflater mInflater = LayoutInflater.from(context);
		View view = mInflater.inflate(R.layout.dialog_img_choice, null);
		LinearLayout tvFriends = (LinearLayout) view
				.findViewById(R.id.ll_album);
		tvFriends.setOnClickListener(this);
		LinearLayout tvFriendsCircle = (LinearLayout) view
				.findViewById(R.id.ll_camera);
		tvFriendsCircle.setOnClickListener(this);
		Button btnCancle = (Button) view.findViewById(R.id.btn_cancel);
		btnCancle.setOnClickListener(this);
		dialogHead = new Dialog(context, R.style.dialogWindowStyle);
		dialogHead.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dialogHead.setContentView(view, new LayoutParams(
				LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
	}

	/**
	 * 选择图片裁剪
	 */
	private void startImagePick() {
		Intent intent;
		if (Build.VERSION.SDK_INT < 19) {
			intent = new Intent();
			intent.setAction(Intent.ACTION_GET_CONTENT);
			intent.setType("image/*");
			startActivityForResult(Intent.createChooser(intent, "选择图片"),
					ImageUtils.REQUEST_CODE_GETIMAGE_BYCROP);
		} else {
			intent = new Intent(Intent.ACTION_PICK,
					Images.Media.EXTERNAL_CONTENT_URI);
			intent.setType("image/*");
			startActivityForResult(Intent.createChooser(intent, "选择图片"),
					ImageUtils.REQUEST_CODE_GETIMAGE_BYCROP);
		}
	}

	/**
	 * 相机拍照
	 */
	private void startActionCamera() {
		Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
		intent.putExtra(MediaStore.EXTRA_OUTPUT, this.getCameraTempFile());
		startActivityForResult(intent,
				ImageUtils.REQUEST_CODE_GETIMAGE_BYCAMERA);
	}

	// 拍照保存的绝对路径
	private Uri getCameraTempFile() {
		String storageState = Environment.getExternalStorageState();
		if (storageState.equals(Environment.MEDIA_MOUNTED)) {
			File savedir = new File(FILE_SAVEPATH);
			if (!savedir.exists()) {
				savedir.mkdirs();
			}
		} else {
			showToast("无法保存上传的头像，请检查SD卡是否挂载");
			return null;
		}
		String timeStamp = new SimpleDateFormat("yyyyMMddHHmmss")
				.format(new Date());
		// 照片命名
		String cropFileName = "osc_camera_" + timeStamp + ".jpg";
		// 裁剪头像的绝对路径
		protraitPath = FILE_SAVEPATH + cropFileName;
		protraitFile = new File(protraitPath);
		cropUri = Uri.fromFile(protraitFile);
		this.origUri = this.cropUri;
		return this.cropUri;
	}

	/**
	 * 拍照后裁剪
	 * 
	 * @param data
	 *            原始图片
	 */
	private void startActionCrop(Uri data) {
		Intent intent = new Intent("com.android.camera.action.CROP");
		intent.setDataAndType(data, "image/*");
		intent.putExtra("output", this.getUploadTempFile(data));
		intent.putExtra("crop", "true");
		intent.putExtra("aspectX", 1);// 裁剪框比例
		intent.putExtra("aspectY", 1);
		intent.putExtra("outputX", CROP);// 输出图片大小
		intent.putExtra("outputY", CROP);
		intent.putExtra("scale", true);// 去黑边
		intent.putExtra("scaleUpIfNeeded", true);// 去黑边
		startActivityForResult(intent,
				ImageUtils.REQUEST_CODE_GETIMAGE_BYSDCARD);
	}

	// 裁剪头像的绝对路径
	private Uri getUploadTempFile(Uri uri) {
		String storageState = Environment.getExternalStorageState();
		if (storageState.equals(Environment.MEDIA_MOUNTED)) {
			File savedir = new File(FILE_SAVEPATH);
			if (!savedir.exists()) {
				savedir.mkdirs();
			}
		} else {
			showToast("无法保存上传的头像，请检查SD卡是否挂载");
			return null;
		}
		String timeStamp = new SimpleDateFormat("yyyyMMddHHmmss")
				.format(new Date());
		String thePath = ImageUtils.getAbsolutePathFromNoStandardUri(uri);
		// 如果是标准Uri
		if (StringUtils.isEmpty(thePath)) {
			thePath = ImageUtils.getAbsoluteImagePath(this, uri);
		}
		String ext = FileUtils.getFileFormat(thePath);
		ext = StringUtils.isEmpty(ext) ? "jpg" : ext;
		// 照片命名
		String cropFileName = "osc_crop_" + timeStamp + "." + ext;
		// 裁剪头像的绝对路径
		protraitPath = FILE_SAVEPATH + cropFileName;
		protraitFile = new File(protraitPath);
		cropUri = Uri.fromFile(protraitFile);
		return this.cropUri;
	}

	@Override
	protected void onActivityResult(final int requestCode,
			final int resultCode, final Intent data) {
		if (resultCode != RESULT_OK)
			return;
		switch (requestCode) {
		case ImageUtils.REQUEST_CODE_GETIMAGE_BYCAMERA:
			startActionCrop(origUri);// 拍照后裁剪
			break;
		case ImageUtils.REQUEST_CODE_GETIMAGE_BYCROP:
			startActionCrop(data.getData());// 选图后裁剪
			break;
		case ImageUtils.REQUEST_CODE_GETIMAGE_BYSDCARD:
			showProgress();
			uploadNewPhoto();// 上传头像
			break;
		}
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		super.onClick(v);
		switch (v.getId()) {
		case R.id.iv_back:
			this.finish();
			break;
		case R.id.ll_bg:
			img_head_perfection_click();
			break;
		case R.id.tv_right:
			btn_getData();
			break;
		case R.id.ll_date:
			showDateDialog();
			break;
		case R.id.ll_time:
			showTimeDialog();
			break;
		case R.id.ll_price:
			showPriceDialog();
			break;
		case R.id.ll_red_packet:
			if (isFlag_red) {
				iv_red_packet_choice.setImageResource(R.drawable.mainstar_gou_2);
			}else {
				iv_red_packet_choice.setImageResource(R.drawable.mainstar_gou_1);
			}
			isFlag_red = !isFlag_red;
			break;
		case R.id.ll_gift:
			if (isFlag_gift) {
				iv_gift_choice.setImageResource(R.drawable.mainstar_gou_2);
			}else {
				iv_gift_choice.setImageResource(R.drawable.mainstar_gou_1);
			}
			isFlag_gift = !isFlag_gift;
			break;
		case R.id.ll_album:
			if (null != dialogHead) {
				dialogHead.dismiss();
				startImagePick();
			}
			break;
		case R.id.ll_camera:
			if (null != dialogHead) {
				dialogHead.dismiss();
				startActionCamera();
			}
			break;
		case R.id.btn_cancel:
			if (null != dialogHead) {
				dialogHead.dismiss();
			}
			break;
		default:
			break;
		}
	}

	private void btn_getData() {
		String str_date = tv_date.getText().toString().trim();
		String str_time= tv_time.getText().toString().trim();
		String price= tv_price.getText().toString().trim();
		String str_price="";
		if (!TextUtils.isEmpty(price)) {
			String strs[]=price.split("元");
			str_price = strs[0];
		}
		
		if (TextUtils.isEmpty(str_date)) {
			showToast("请选择日期");
			return;
		}
		if (TextUtils.isEmpty(str_time)) {
			showToast("请选择开播时间");
			return;
		}
		if (TextUtils.isEmpty(str_price)) {
			showToast("请选择价格");
			return;
		}
		
		String redpacket="0";
		if (isFlag_red) {
			redpacket= "10";
		}
		String gift="0";
		if (isFlag_gift) {
			gift= "10";
		}
		
		getPublishData(str_date,str_time,str_price,redpacket,gift);
	}
}
